package com.centon.auto_code.project.service.impl;

import com.centon.auto_code.comm.ServiceCode;
import com.centon.auto_code.domain.ProMicroService;
import com.centon.auto_code.domain.gen.ProMicroServiceCriteria;
import com.centon.auto_code.mapper.ProMicroServiceMapper;
import com.centon.auto_code.project.service.ProMicroServiceService;
import com.centon.auto_code.project.vo.ProMicroServiceVo;
import com.centon.auto_code.util.BeanUtil;
import com.centon.auto_code.util.CommUtils;
import com.centon.auto_code.vo.BaseRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProMicroServiceServiceImpl implements ProMicroServiceService {

    @Autowired
    private ProMicroServiceMapper proMicroServiceMapper;


    @Override
    public BaseRes<List<ProMicroServiceVo>> microList(ProMicroServiceVo proMicroServiceVo) {

        BaseRes<List<ProMicroServiceVo>> res = new BaseRes<>();
        ProMicroServiceCriteria criteria = new ProMicroServiceCriteria();
        criteria.createCriteria().andDelFlagEqualTo(0).andProjectIdEqualTo(proMicroServiceVo.getProjectId());

        List<ProMicroService> proMicroServices = proMicroServiceMapper.selectByExample(criteria);

        if (proMicroServices.size() > 0){
            List<ProMicroServiceVo> proMicroServiceVos = BeanUtil.voConvertList(proMicroServices, ProMicroServiceVo.class);
            res.setData(proMicroServiceVos);
        }
        res.setServiceCode(ServiceCode.SUCCESS);

        return res;
    }

    @Override
    public BaseRes<ProMicroServiceVo> microDetail(ProMicroServiceVo ProMicroServiceVo) {
        BaseRes<ProMicroServiceVo> res = new BaseRes<>();
        ProMicroService proMicroService = proMicroServiceMapper.selectByPrimaryKey(ProMicroServiceVo.getId());

        if (proMicroService != null){
            ProMicroServiceVo proMicroServiceVo = BeanUtil.voConvert(proMicroService, ProMicroServiceVo.class);
            res.setData(proMicroServiceVo);
        }
        res.setServiceCode(ServiceCode.SUCCESS);
        return res;
    }

    @Override
    public BaseRes<String> updateMicro(ProMicroServiceVo proMicroServiceVo) {
        proMicroServiceVo.setUpdateTime(new Date());
        proMicroServiceMapper.updateByPrimaryKeySelective(proMicroServiceVo);
        return  new BaseRes<>(ServiceCode.SUCCESS);
    }

    @Override
    public BaseRes<String> deleteMicro(ProMicroServiceVo proMicroServiceVo) {
        proMicroServiceVo.setDelFlag(1);
        proMicroServiceMapper.updateByPrimaryKeySelective(proMicroServiceVo);
        return new BaseRes<>(ServiceCode.SUCCESS);
    }

    @Override
    public BaseRes<String> addMicro(ProMicroServiceVo proMicroServiceVo) {
        proMicroServiceVo.setId(CommUtils.randomUUID());
        proMicroServiceVo.setCreateTime(new Date());
        proMicroServiceMapper.insertSelective(proMicroServiceVo);
        return new BaseRes<>(ServiceCode.SUCCESS);
    }
}
